﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using fideeraSoft.Utils;
using System.Windows.Forms;
using fideeraSoft.Entities;

namespace fideeraSoft.Paneles
{
    public class ProductoPanelAlta:PanelGeneral
    {
        private System.Windows.Forms.GroupBox gbGral;
        private System.Windows.Forms.Label lbId;
        private System.Windows.Forms.Label lbIdProducto;
        private System.Windows.Forms.Label lbNombre;
        private System.Windows.Forms.TextBox tbNombre;
        private System.Windows.Forms.TextBox tbDescripcion;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.Button btSubmit;
        private System.Windows.Forms.Label lbDescripcion;
        private System.Windows.Forms.TableLayoutPanel tlpGral;
        private static int groupWidth = 800;
        private static int groupHeight = 500;
        private Label lbCodigo;
        private Label lbColor;
        private TextBox tbCaracteristicas;
        private TextBox tbCodigo;
        private Label lbMarca;
        private TextBox tbMarca;

        private int posicionTablaBotones;


        public ProductoPanelAlta(Handler handler)
        {
            
            //MetodosGenerales.cargarImagenEnPanel(this);

            this.formatearPanelGeneral(handler);

            this.InitializeComponent();

            this.formatearTableTitulo("ALTA DE PRODUCTO");

            formatearGroupBox();
            formatTableForm();
            formatearTablaBotones();
            
            lbIdProducto.Text = this.handler.buscarNuevoUltimoIdProductoEnBD().ToString();
                                    
            this.Controls.Add(gbGral);
            this.Controls.Add(tlpBotones);
        }


        #region FORMATEO 
        private void formatearGroupBox()
        {
            gbGral.Size = new Size(groupWidth, groupHeight);
            
            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, gbGral.Size.Width), this.InitialRealPointVertical);
            
            gbGral.Controls.Add(tlpGral);
        }

        
        private void formatTableForm()
        {
                        
            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpGral.Size.Width), 30);

            cargarControlesEnTabla();

        }
        

        private void formatearTablaBotones()
        {
            
            tlpBotones.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBotones.BackColor = Color.LightCyan;

            posicionTablaBotones = this.InitialRealPointVertical + gbGral.Size.Height + 20;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), posicionTablaBotones);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btSubmit);

        }

        #endregion


        private void cargarControlesEnTabla()
        {

            tlpGral.Controls.Add(lbId);
            tlpGral.Controls.Add(lbIdProducto);

            tlpGral.Controls.Add(lbNombre);
            tlpGral.Controls.Add(tbNombre);

            tlpGral.Controls.Add(lbCodigo);
            tlpGral.Controls.Add(tbCodigo);

            tlpGral.Controls.Add(lbDescripcion);
            tlpGral.Controls.Add(tbDescripcion);

            tlpGral.Controls.Add(lbColor);
            tlpGral.Controls.Add(tbCaracteristicas);

            tlpGral.Controls.Add(lbMarca);
            tlpGral.Controls.Add(tbMarca);


        }

        
        private void InitializeComponent()
        {
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.lbId = new System.Windows.Forms.Label();
            this.lbIdProducto = new System.Windows.Forms.Label();
            this.lbNombre = new System.Windows.Forms.Label();
            this.tbNombre = new System.Windows.Forms.TextBox();
            this.tbDescripcion = new System.Windows.Forms.TextBox();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.btSubmit = new System.Windows.Forms.Button();
            this.lbDescripcion = new System.Windows.Forms.Label();
            this.lbCodigo = new System.Windows.Forms.Label();
            this.lbColor = new System.Windows.Forms.Label();
            this.tbCaracteristicas = new System.Windows.Forms.TextBox();
            this.tbCodigo = new System.Windows.Forms.TextBox();
            this.lbMarca = new System.Windows.Forms.Label();
            this.tbMarca = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // tlpGral
            // 
            this.tlpGral.BackColor = System.Drawing.Color.LightCyan;
            this.tlpGral.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpGral.ColumnCount = 2;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 70F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 6;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 16.66F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 16.66F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 16.66F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 16.66F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 16.66F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 16.66F));
            this.tlpGral.Size = new System.Drawing.Size(700, 240);
            this.tlpGral.TabIndex = 0;
            // 
            // gbGral
            // 
            this.gbGral.Font = new System.Drawing.Font("Bodoni MT", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(800, 400);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Alta de Producto";
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(0, 0);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(100, 23);
            this.lbId.TabIndex = 0;
            this.lbId.Text = "ID de Producto";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbIdProducto
            // 
            this.lbIdProducto.AutoSize = true;
            this.lbIdProducto.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdProducto.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdProducto.Location = new System.Drawing.Point(0, 0);
            this.lbIdProducto.Name = "lbIdProducto";
            this.lbIdProducto.Size = new System.Drawing.Size(100, 23);
            this.lbIdProducto.TabIndex = 0;
            this.lbIdProducto.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lbNombre
            // 
            this.lbNombre.AutoSize = true;
            this.lbNombre.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbNombre.Location = new System.Drawing.Point(0, 0);
            this.lbNombre.Name = "lbNombre";
            this.lbNombre.Size = new System.Drawing.Size(100, 23);
            this.lbNombre.TabIndex = 0;
            this.lbNombre.Text = "Nombre/Variedad";
            this.lbNombre.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbNombre
            // 
            this.tbNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbNombre.Location = new System.Drawing.Point(0, 0);
            this.tbNombre.MaxLength = 50;
            this.tbNombre.Name = "tbNombre";
            this.tbNombre.Size = new System.Drawing.Size(250, 30);
            this.tbNombre.TabIndex = 0;
            // 
            // tbDescripcion
            // 
            this.tbDescripcion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbDescripcion.Location = new System.Drawing.Point(0, 0);
            this.tbDescripcion.MaxLength = 150;
            this.tbDescripcion.Name = "tbDescripcion";
            this.tbDescripcion.Size = new System.Drawing.Size(500, 30);
            this.tbDescripcion.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // btSubmit
            // 
            this.btSubmit.BackColor = System.Drawing.Color.AliceBlue;
            this.btSubmit.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btSubmit.Location = new System.Drawing.Point(0, 0);
            this.btSubmit.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btSubmit.Name = "btSubmit";
            this.btSubmit.Size = new System.Drawing.Size(75, 40);
            this.btSubmit.TabIndex = 0;
            this.btSubmit.Text = "Guardar";
            this.btSubmit.UseVisualStyleBackColor = false;
            this.btSubmit.Click += new System.EventHandler(this.btSubmit_Click);
            // 
            // lbDescripcion
            // 
            this.lbDescripcion.AutoSize = true;
            this.lbDescripcion.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbDescripcion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbDescripcion.Location = new System.Drawing.Point(0, 0);
            this.lbDescripcion.Name = "lbDescripcion";
            this.lbDescripcion.Size = new System.Drawing.Size(100, 23);
            this.lbDescripcion.TabIndex = 0;
            this.lbDescripcion.Text = "Descripción";
            this.lbDescripcion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbCodigo
            // 
            this.lbCodigo.AutoSize = true;
            this.lbCodigo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCodigo.Location = new System.Drawing.Point(0, 0);
            this.lbCodigo.Name = "lbCodigo";
            this.lbCodigo.Size = new System.Drawing.Size(100, 23);
            this.lbCodigo.TabIndex = 0;
            this.lbCodigo.Text = "Código";
            this.lbCodigo.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbColor
            // 
            this.lbColor.AutoSize = true;
            this.lbColor.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbColor.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbColor.Location = new System.Drawing.Point(0, 0);
            this.lbColor.Name = "lbColor";
            this.lbColor.Size = new System.Drawing.Size(100, 23);
            this.lbColor.TabIndex = 0;
            this.lbColor.Text = "Características";
            this.lbColor.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbCaracteristicas
            // 
            this.tbCaracteristicas.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCaracteristicas.Location = new System.Drawing.Point(0, 0);
            this.tbCaracteristicas.MaxLength = 150;
            this.tbCaracteristicas.Name = "tbCaracteristicas";
            this.tbCaracteristicas.Size = new System.Drawing.Size(500, 30);
            this.tbCaracteristicas.TabIndex = 0;
            // 
            // tbCodigo
            // 
            this.tbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCodigo.Location = new System.Drawing.Point(0, 0);
            this.tbCodigo.MaxLength = 10;
            this.tbCodigo.Name = "tbCodigo";
            this.tbCodigo.Size = new System.Drawing.Size(140, 30);
            this.tbCodigo.TabIndex = 0;
            // 
            // lbMarca
            // 
            this.lbMarca.AutoSize = true;
            this.lbMarca.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbMarca.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbMarca.Location = new System.Drawing.Point(0, 0);
            this.lbMarca.Name = "lbMarca";
            this.lbMarca.Size = new System.Drawing.Size(100, 23);
            this.lbMarca.TabIndex = 0;
            this.lbMarca.Text = "Marca";
            this.lbMarca.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbMarca
            // 
            this.tbMarca.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbMarca.Location = new System.Drawing.Point(0, 0);
            this.tbMarca.MaxLength = 50;
            this.tbMarca.Name = "tbMarca";
            this.tbMarca.Size = new System.Drawing.Size(250, 30);
            this.tbMarca.TabIndex = 0;
            // 
            // ProductoPanelAlta
            // 
            this.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ResumeLayout(false);

        }

        private void btSubmit_Click(object sender, EventArgs e)
        {            
            if (this.validarTextboxProducto())
            {   
                    Producto producto = new Producto();

                    try
                    {
                        cargarTextBoxEnProducto(producto);
                    }
                    catch (MiException ex)
                    {
                        //  CLogger.Append("[btSubmit_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
                        MessageBox.Show("Error al dar de alta Producto - Datos Incompletos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    try
                    {
                        if (handler.buscarProductoPorNombreYMarca(producto.Nombre,producto.Marca) == null)
                        {
                                this.handler.altaProducto(producto);
                                MessageBox.Show("Nuevo Producto creado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                limpiarTextBoxs();

                                lbIdProducto.Text = this.handler.buscarNuevoUltimoIdProductoEnBD().ToString();                                                        
                        }
                        else
                        {
                            tbNombre.Clear();
                            tbMarca.Clear();
                            MessageBox.Show("Ya existe un Producto con ese nombre y marca \nIngrese otro nombre de producto", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }


                    }
                    catch (MiException ex)
                    {
                        CLogger.Append("[btSubmit_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
                        MessageBox.Show("Error al dar de alta Producto - Datos Incompletos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
            }
            else
            {
                MessageBox.Show("Datos vacios o incompletos, revisar datos necesarios de Producto", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void limpiarTextBoxs()
        {
            tbNombre.Clear();
            tbDescripcion.Clear();
            tbCodigo.Clear();
            tbCaracteristicas.Clear();
            tbMarca.Clear();
            
        }

        private void cargarTextBoxEnProducto(Producto producto)     //FD ya existe la validación, solo se debería copiar
        {
            if (!string.IsNullOrWhiteSpace(lbIdProducto.Text)) producto.IdProducto = Convert.ToInt32(lbIdProducto.Text); else throw new Exception("Exception en ID de producto");
            if(!string.IsNullOrWhiteSpace(tbNombre.Text)) producto.Nombre=tbNombre.Text; else throw new Exception("Exception en Nombre de producto");
            if(!string.IsNullOrWhiteSpace(tbDescripcion.Text)) producto.Descripcion=tbDescripcion.Text; else throw new Exception("Exception en Descripcion de producto");
            if (!string.IsNullOrWhiteSpace(tbCodigo.Text)) producto.Codigo = tbCodigo.Text; else producto.Codigo="";
            if (!string.IsNullOrWhiteSpace(tbCaracteristicas.Text)) producto.Color = tbCaracteristicas.Text; else producto.Color = "";
            if (!string.IsNullOrWhiteSpace(tbMarca.Text)) producto.Marca = tbMarca.Text; else throw new Exception("Exception en Marca de producto");
        }

        private bool validarTextboxProducto()
        {
            return ((!string.IsNullOrWhiteSpace(tbNombre.Text)) && (!string.IsNullOrWhiteSpace(tbDescripcion.Text))&&(!string.IsNullOrWhiteSpace(tbMarca.Text))&&(!string.IsNullOrWhiteSpace(lbIdProducto.Text)));
                //&& (!string.IsNullOrWhiteSpace(tbColor.Text)) && (!string.IsNullOrWhiteSpace(tbCodigo.Text)) );
        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

    }
}
